package com.example.udptestdemo.servers;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.SocketException;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.udptestdemo.enums.UdpSocketDataType;
import com.example.udptestdemo.events.SendResumeFileEvent;
import com.example.udptestdemo.listeners.FinishListener;
import com.example.udptestdemo.listeners.ProgressListener;
import com.example.udptestdemo.runnables.MonitorRunnable;
import com.example.udptestdemo.runnables.RecvFileRunnable;
import com.example.udptestdemo.runnables.SendFileRunnable;
import com.example.udptestdemo.runnables.SendResumeFileRunnable;

public class ADPlayerSocket extends BaseSocket {

	private SendResumeFileRunnable mSendResumeFileRunnable;
	private SendFileRunnable mSendFileRunnable;
	private RecvFileRunnable mRecvFileRunnable;

	private MonitorRunnable mMonitorRunnable;

	public ADPlayerSocket(int bindPort) throws SocketException {
		super(bindPort);

	}

	public ADPlayerSocket(int bindPort, int sotimeout) throws SocketException {
		super(bindPort, sotimeout);

	}

	@Override
	public void startWork() {
		super.startWork();
	}

	@Override
	protected void dealWithData(DatagramPacket packet) {
		byte[] recv_buffer = packet.getData();
		int real_len = packet.getLength();
		if (real_len > 2) {
			if (recv_buffer[0] + recv_buffer[1] == -1) {
				byte type = recv_buffer[0];
				if (type == UdpSocketDataType.COMMAND) { // 控制命令
					String str = new String(recv_buffer, 10, real_len);
					_dealwithCommand(str, packet.getAddress(), packet.getPort());
				} else if (type == UdpSocketDataType.FILE_SEND) { // 文件
					if(mSendFileRunnable!=null && mSendFileRunnable.getRunStatus())
						mSendFileRunnable.dealWithData(recv_buffer, real_len);
				}else if(type ==UdpSocketDataType.FILE_RECIVE){ 
					if(mRecvFileRunnable!=null&& mRecvFileRunnable.getRunStatus())
						mRecvFileRunnable.whriteData(recv_buffer, real_len);
				}else if (type == UdpSocketDataType.FILE_RESUME_SEND) { // 文件断点续传
					if (mSendResumeFileRunnable != null && mSendResumeFileRunnable.getRunStatus())
						mSendResumeFileRunnable.dealWithData(recv_buffer, real_len);
				}else if(type==UdpSocketDataType.FILE_RESUME_RECIVE){ //文件断点续传接收
					
				} else if (type == UdpSocketDataType.MONITOR) { // 监控
					if (mMonitorRunnable != null && mMonitorRunnable.getRunStatus()) {
						mMonitorRunnable.dealWithData(recv_buffer, real_len);
					}
				}
			}

		}

	}

	private void _dealwithCommand(final String str, InetAddress address, int port) {

		JSONObject json;
		try {
			json = new JSONObject(str);
			int cmd = json.getInt("cmd");
			if (2 == cmd) { // 发送文件应答
				String file_name = json.getString("file_name");
				String file_path = json.getString("file_path");
				String file_md5 = json.getString("file_md5");
				long recived_file_size = json.getLong("recived_file_size");
				SendResumeFileEvent event = new SendResumeFileEvent();
				event.FileName = file_name;
				event.FilePath = file_path;
				event.Md5 = file_md5;
				event.mRemoteAddress = address;
				event.mRemotePort = port;
				event.RecivedFileSize = recived_file_size;
				EventBus.getDefault().post(event);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	public void setSendResumeFileRunnable(SendResumeFileRunnable runnable) {
		this.mSendResumeFileRunnable = runnable;
	}

	public void setSendFileRunnable(SendFileRunnable runnable){
		this.mSendFileRunnable = runnable;
	}
	
	public void setRecvFileRunnable(RecvFileRunnable runnable){
		this.mRecvFileRunnable = runnable;
	}

	public void setMonitorRunnable(MonitorRunnable runnable) {
		this.mMonitorRunnable = runnable;
	}


	public void sendCommand() {

	}

	public void runScreenWatch() {

	}

}
